using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace ITSP.EventReceiverConfig
{
    public class ProgramControl : System.Object
    {
        public enum Mode
        {
            List,
            Add,
            Remove
        }

        private Mode ProgramMode;
        private string _url;
        private EventReceiverConfiguration _configuration;
        private string _programOutput;

        /// <summary>
        /// 
        /// </summary>
        public string ProgramOutput
        {
            get { return _programOutput; }
        }

        public ProgramControl()
        {
            _configuration = new EventReceiverConfiguration();
        }

        public void DisplayUsage()
        {
            try
            {
                StringBuilder displayUsageOutput = new StringBuilder();
                displayUsageOutput.AppendLine(""); 
                displayUsageOutput.AppendLine("ITSP.EventReceiverConfig (http://itspeventreceiver.codeplex.com");
                displayUsageOutput.AppendLine("-----------------------------------------------------------------------");
                displayUsageOutput.AppendLine("");
                displayUsageOutput.AppendLine("Usage");
                displayUsageOutput.AppendLine("------");
                displayUsageOutput.AppendLine("itsp.eventreceiverconfig.exe");
                displayUsageOutput.AppendLine("-mode [Add/Remove/List]");
                displayUsageOutput.AppendLine("-url [weburl] (e.g http://sharepoint)");
                displayUsageOutput.AppendLine("-targettype [List/Content Type/Assembly]]");
                displayUsageOutput.AppendLine("-targetname [Name of List/Content Type/Assembly ]\t");
                displayUsageOutput.AppendLine("-assembly [Full Assembly Name] (e.g 'Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKey=')");
                displayUsageOutput.AppendLine("-class [Class  Name] (e.g 'SPItemEventReceiver')");
                displayUsageOutput.AppendLine("-name [Event Receiver Name] (e.g 'ItemEventReceiver')");
                displayUsageOutput.AppendLine("-type [SPEventReceiverType] (e.g 'ItemAdded/ItemAdding')");
                displayUsageOutput.AppendLine("-sequence [number 10000+] (e.g '10010')");
                displayUsageOutput.AppendLine("");
                displayUsageOutput.AppendLine("");
                displayUsageOutput.AppendLine("Syntax: Add Mode");
                displayUsageOutput.AppendLine("------------------");
                displayUsageOutput.AppendLine("itsp.eventreceiverconfig");
                displayUsageOutput.AppendLine("-mode Add -url [weburl] -targettype [List/ContentType] -targetname [List/Content Type Name] -assembly [Full Assembly Name] -class [Class Name] -name [Event Receiver Name] -type ItemUpdated -sequence [number 10000+]");
                displayUsageOutput.AppendLine("");
                displayUsageOutput.AppendLine("Syntax: Remove Mode");
                displayUsageOutput.AppendLine("------------------");
                displayUsageOutput.AppendLine("itsp.eventreceiverconfig");
                displayUsageOutput.AppendLine("-mode Remove -url [weburl] -targettype [List/ContentType] -targetname [List/Content Type Name] -assembly [Full Assembly Name] -class [Class Name] -name [Event Receiver Name]");
                displayUsageOutput.AppendLine("");
                displayUsageOutput.AppendLine("Syntax: List Mode");
                displayUsageOutput.AppendLine("------------------");
                displayUsageOutput.AppendLine("itsp.eventreceiverconfig");
                displayUsageOutput.AppendLine("-mode List -url [weburl] -targettype [List/ContentType/Assembly] -targetname [Assembly/List/Content Type Name] -assembly [Full Assembly Name]");
                displayUsageOutput.AppendLine("");
                displayUsageOutput.AppendLine("-----------------------------------------------------------------------");
                System.Console.Write(displayUsageOutput.ToString());
            }
            catch (Exception ex)
            {
                //don't do anything
            }
        }

        public void SetProgramMode(Mode mode)
        {
            ProgramMode = mode;
        }

        public void SetContext(string sUrl)
        {
            Uri uriSite;
            if (!Uri.TryCreate(sUrl, UriKind.RelativeOrAbsolute, out uriSite))
                throw new ApplicationException("Please provide valid url. Url specified: " + sUrl);
            _url = sUrl;
        }

        public void SetConfiguration(EventReceiverConfiguration config)
        {
            _configuration = config;
        }

        public void DoTask()
        {
            try
            {
                try
                {
                    IEventReceiverOperation eventReceiverOperation = EventReceiverOperationFactory.GetOperationInstance(ProgramMode,_url, _configuration);
                    if(eventReceiverOperation!=null)
                    {
                        if(!eventReceiverOperation.Execute())
                        {
                            throw new ApplicationException("The operation failed");
                        }
                        else
                        {
                            _programOutput = eventReceiverOperation.GetOutput();
                            
                        }
                    }

                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    //output to console
                    System.Console.Write(ProgramOutput);
                }

            }
            catch(System.IO.FileNotFoundException fileex)
            {
                Trace.WriteLine("FileNotFoundException Exception: " + fileex.Message + " : " + fileex.StackTrace);
                throw fileex;
            }
            catch (ApplicationException appex)
            {
                Trace.WriteLine("Application Exception: " + appex.Message + " : " + appex.StackTrace);
                throw appex;
            }
            
           

        }
        
    }
}
